%% loadProblem.m
% This is a script to take in mat files containing parameter values, put
% them into appropriate A and B matrices, and then declare values of the
% MPC problem such as R_i, Tlo, Thi, ulo, uhi.
% It also loads a disturbance profile from MPC load data currently.
% The problem is
%           min( U'*R*U) 
%           s.t. X(k+1) = AX(k) + Bu(k) + d(k)
%                X(k) \in [Tlo, Thi]
%                u(k) \in [ulo, uhi]
%                X(0) = X(t)
% where U = [u_1(1)...u_1(N),u_2(1),...u_2(N),...,u_n(1),...u_n(N)]'
% and u(k) = [u_1(k),u_2(k),...u_n(k)]
% and X(k) = [x_1(k),x_2(k),...x_n(k)]
% and R = diag(R_1*eye(N),...R_n*eye(N))
% and N = the prediction horizon
% and n = the number of zones (we'll only go up to 9 here at maximum)
DEBUG = false;
close all
        

%% Set up MPC Problem
% Set up MPC parameters
dt = 1/24/60; % sample time is 1 minute = (1 day / 24 hours / 60 minutes)
% N = 60*3; % 3 hour prediction horizon
N = 180; % small size so Matlab can handle this
R = 0.1; 
n = 1;

% Degrees C
Tlo = 20;
Thi = 23;
% ulo = 10;
% uhi = 30; 
ulo = -10;
uhi = 10;


% Initial temperature
T0 = [22.9,22.8,22.8]';

% Initial control
% u0 = 22;

% load disturbance, currently only loads load data from MPC lab
d = loadDisturbance(dt,n,0);
tend = length(d);

% Only run 50 minutes of the day with MPC because it takes too long
tendtemp = 50;